package com.juli.v2xdemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.CoordinateConverter;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CustomMapStyleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.animation.AlphaAnimation;
import com.amap.api.maps.model.animation.Animation;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.cz.basetool.CodeTool;
import com.github.capur16.digitspeedviewlib.DigitSpeedView;
import com.juli.v2xdemo.R;
import com.juli.v2xdemo.entity.CarInfo;
import com.juli.v2xdemo.entity.MessageEvent;
import com.juli.v2xdemo.service.Location_Service;
import com.juli.v2xdemo.udp.UDPSocket;
import com.juli.v2xdemo.util.CoodUtils;
import com.juli.v2xdemo.util.GPSutils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

//顶部是 -700
//中间0
//底部750   距离*2.33
public class MainActivity3 extends Activity {


    //    @BindView(R.id.mountain)
//    ImageView mountain;
    @BindView(R.id.tv_juli)
    TextView tvJuli;
    @BindView(R.id.im_yindao)
    ImageView imYindao;
    @BindView(R.id.tv_cheliangyujing)
    TextView tvCheLiangYuJing;

    @BindView(R.id.digit_speed_view)
    DigitSpeedView digitSpeedView;

    @BindView(R.id.tv_traffic)
    TextView tvTraffic;
    @BindView(R.id.layout_traffic_sign)//交通标志
            LinearLayout layouTrafficSign;
    @BindView(R.id.iv_vehicle_qianfangcheliang)
    ImageView ivVehicleQianfangcheliang;
    @BindView(R.id.iv_vehicle_houchemangqu)
    ImageView ivVehicleHouchemangqu;
    @BindView(R.id.iv_vehicle_jingjicheliang)
    ImageView ivVehicleJingjicheliang;
    @BindView(R.id.iv_vehicle_guzhangcheliang)
    ImageView ivVehicleGuzhangcheliang;
    @BindView(R.id.im_hongdeng)
    ImageView imHongdeng;
    @BindView(R.id.tv_hongdengtime)
    TextView tvHongdengtime;

    @BindView(R.id.tv_hongdengtimename)
    TextView tv_hongdengtimename;

    @BindView(R.id.tv_lvdi)
    TextView tvLvdi;
    @BindView(R.id.tv_lvdi2)
    TextView tvLvdi2;

    @BindView(R.id.im_hongdeng1)
    ImageView imHongdeng1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        planeWar = (PlaneWar) View.inflate(this, R.layout.activity_main3, null);
        setContentView(R.layout.activity_main3);
        init(savedInstanceState);
        ButterKnife.bind(this);

        EventBus.getDefault().register(this);
        createClient();
//        mountain.setVisibility(View.GONE);
        initview();
        startService(new Intent(this, Location_Service.class));

//

//
//        shezhizhongxin(31.2766666,121.1582060,1800);
    }

    UDPSocket socket;

    /**
     * 创建客户端
     */
    private void createClient() {
        socket = new UDPSocket(this);
        socket.startUDPSocket();
    }

    void initview() {
        //前方车辆碰撞预警
        Glide.with(MainActivity3.this).load(R.drawable.ic_qfpzyj).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(ivVehicleQianfangcheliang);
        //后方盲区超车预警
        Glide.with(MainActivity3.this).load(R.drawable.ic_hfmqyj).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(ivVehicleHouchemangqu);
        //紧急车辆预警
        Glide.with(MainActivity3.this).load(R.drawable.ic_jjclyj).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(ivVehicleJingjicheliang);
        //故障车辆
        Glide.with(MainActivity3.this).load(R.drawable.ic_gzclyj).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(ivVehicleGuzhangcheliang);

        ivVehicleQianfangcheliang.setVisibility(View.GONE);
        ivVehicleHouchemangqu.setVisibility(View.GONE);
        ivVehicleJingjicheliang.setVisibility(View.GONE);
        ivVehicleGuzhangcheliang.setVisibility(View.GONE);

//=====================右下================
        imHongdeng.setVisibility(View.GONE);
        tvHongdengtime.setVisibility(View.GONE);
        tvLvdi.setVisibility(View.GONE);
        tvLvdi2.setVisibility(View.GONE);
        imHongdeng1.setVisibility(View.GONE);
        tv_hongdengtimename.setVisibility(View.GONE);
    }


    ///////////
    double mycarlatitude = 0;
    double mycarlongitude = 0;
    int qq = 0;

    /**
     * 订阅者处理事件   将数据显示到页面上
     */
    StringBuilder strB = new StringBuilder();
    int q = 0;
    int r = 0;
    int ui = 0;

    public void onEventMainThread(MessageEvent event) {
        Log.e("MessageEvent", "xxxx");

        if (!TextUtils.isEmpty(event.getType())) {
            switch (event.getType()) {
                case "data":
                    if (!TextUtils.isEmpty(event.getType())) {

                        Log.e("MessageEvent", "event" + event.getMessage());
                        CarInfo carData = event.getCarInfo();
                        String vehichlInfo = carData.getVehichlInfo();

                        String lat_long_dir = carData.getLat_Long_Dir();


                        String vehichlInfo8 = CodeTool.oneByteHexNum2BinaryStr8(vehichlInfo);
                        String isme = vehichlInfo8.substring(0, 1);
                        String early_warning = vehichlInfo8.substring(1, 8);
                        String qianfangcheliangpengzhuangyujing = vehichlInfo8.substring(7, 8);
                        String houfangmangquchaoche = vehichlInfo8.substring(6, 7);
                        String jinjicheliang = vehichlInfo8.substring(5, 6);
                        String guzhangcheliang = vehichlInfo8.substring(4, 5);
                        if (isme.equals("0")) {
                            Log.e("车辆预警", "数据：" + early_warning);
                            //自己

                            //==================车况预警开始====================================

                            if (early_warning.equals("0000000")) {
                                ui++;
                                if (ui==20) {
                                    ui=0;
                                    Log.e("车辆预警", "暂无车辆预警");
                                    ivVehicleQianfangcheliang.setVisibility(View.GONE);
                                    ivVehicleHouchemangqu.setVisibility(View.GONE);
                                    ivVehicleJingjicheliang.setVisibility(View.GONE);
                                    ivVehicleGuzhangcheliang.setVisibility(View.GONE);
                                    tvCheLiangYuJing.setTextSize(20);
                                    tvCheLiangYuJing.setText("暂无车辆预警");
                                } else {

                                }


                            } else {
                                if (qianfangcheliangpengzhuangyujing.equals("1")) {
                                    Log.e("车辆预警", "前方车辆碰撞预警");
                                    ivVehicleQianfangcheliang.setVisibility(View.VISIBLE);
                                    strB.append("前方车辆碰撞预警,");
                                } else {
                                    q++;
                                    if (q == 40) {
                                        ivVehicleQianfangcheliang.setVisibility(View.GONE);
                                        q = 0;
                                    } else {

                                    }

                                }

                                if (houfangmangquchaoche.equals("1")) {
                                    Log.e("车辆预警", "后方盲区超车预警");

                                    strB.append("后方盲区超车预警,");
                                    ivVehicleHouchemangqu.setVisibility(View.VISIBLE);
                                } else {
                                    r++;
                                    if (r == 40) {
                                        r = 0;
                                        ivVehicleHouchemangqu.setVisibility(View.GONE);
                                    } else {

                                    }

                                }

                                if (jinjicheliang.equals("1")) {
                                    Log.e("车辆预警", "紧急车辆预警");

                                    strB.append("紧急车辆预警,");
                                    ivVehicleJingjicheliang.setVisibility(View.VISIBLE);
                                } else {
                                    ivVehicleJingjicheliang.setVisibility(View.GONE);
                                }
                                if (guzhangcheliang.equals("1")) {
                                    ivVehicleGuzhangcheliang.setVisibility(View.VISIBLE);
                                    Log.e("车辆预警", "故障车辆预警预警");
                                    strB.append("故障车辆预警预警,");
                                } else {
                                    ivVehicleGuzhangcheliang.setVisibility(View.GONE);
                                }
                                if (strB.toString().endsWith(",")) {
                                    String substring = strB.substring(0, strB.length() - 1);
                                    tvCheLiangYuJing.setTextSize(10);
                                    tvCheLiangYuJing.setText(substring);
                                } else {
                                    tvCheLiangYuJing.setTextSize(10);
                                    tvCheLiangYuJing.setText(strB.toString());
                                }
                                strB.setLength(0);
                            }

                            //===========车况预警结束==============
//
//                            //经度
                            String latitude = GPSutils.longitudeandlatitude(carData.getLatitude());
                            mycarlatitude = Double.parseDouble(latitude);
//                            //纬度
                            String longitude = GPSutils.longitudeandlatitude(carData.getLongitude());
                            mycarlongitude = Double.parseDouble(longitude);
                            Log.e("zzz1", "纬度" + latitude);
                            Log.e("zzz2", "经度" + longitude);
//                            //经纬度方向
                            String Lat_Long_Dir = carData.getLat_Long_Dir();
                            String Lat_Long_Dir8 = CodeTool.oneByteHexNum2BinaryStr8(Lat_Long_Dir);
//                            Log.e("经纬度方向Lat_Long_Dir8", Lat_Long_Dir8);
                            String Heading = carData.getHeading();
                            String substring = Heading.substring(0, 2);
                            String substring2 = Heading.substring(2, 4);
                            Heading = substring2 + substring;
                            int parseInt = Integer.parseInt(Heading, 16);
//                            Log.e("车辆航向角Heading", "原始：" + Heading);
//                            Log.e("车辆航向角Heading", "" + parseInt);
//                            //速度
                            String Speed = carData.getSpeed();
                            String sudu = CoodUtils.Sudu(Speed);
                            qq++;
                            if (qq == 20) {
                                float d = Float.parseFloat(sudu);
//                                double d2=    d/100*3.6;
//                                Log.e("速度", sudu);
                                qq = 0;
                                digitSpeedView.updateSpeed((int) d);
                            }

//


                            String AlertType = carData.getAlertType();
                            String baty1 = AlertType.substring(0, 2);
                            String baty2 = AlertType.substring(2, 4);
//                            Log.e("ZZZZZZZZZZZZZZZZZZZZZ1", "early_warning："+baty2);
                            String baty28 = CodeTool.oneByteHexNum2BinaryStr8(baty2);
                            Log.e("路测预警消息", "数据:" + baty28);
                            int v = Integer.parseInt(baty1);
                            AlertType_Byte1(v);
//
                            AlertType_Byte2(baty28);
                            //警示信息距离
                            String description = carData.getDescription();


                            //车道数目
                            String Lane_num = carData.getLane_num();
                            //当前车辆所在车道
                            String veh_lane = carData.getVeh_Lane();
                            Log.e("我车车道", "" + veh_lane);
                            //经度
//                           mycarlongitude=mycarlongitude+Double.parseDouble("0.004670000");
//                           //纬度
//                           mycarlatitude=mycarlatitude-Double.parseDouble("0.001840782");
                            LatLng latLng = new LatLng(mycarlatitude, mycarlongitude);
                            CoordinateConverter converter = new CoordinateConverter(this);
// CoordType.GPS 待转换坐标类型
                            converter.from(CoordinateConverter.CoordType.GPS);
// 转换
                            converter.coord(latLng);
// 获取转换之后的高德坐标
                            LatLng result = converter.convert();

                            shezhizhongxin(result.latitude, result.longitude, parseInt);
                            //描述
                            Description(description);

                        } else {
                            //当前车辆所在车道
                            //经度
                            String latitude = GPSutils.longitudeandlatitude(carData.getLatitude());
                            double otherlatitude = Double.parseDouble(latitude);
                            //纬度
                            String longitude = GPSutils.longitudeandlatitude(carData.getLongitude());

                            double otherlongitude = Double.parseDouble(longitude);
//                            otherlatitude=otherlatitude+Double.parseDouble("0.004670000");
//                            otherlatitude=otherlatitude-Double.parseDouble("0.001840782");
                            LatLng latLng = new LatLng(otherlatitude, otherlongitude);
                            CoordinateConverter converter = new CoordinateConverter(this);
// CoordType.GPS 待转换坐标类型
                            converter.from(CoordinateConverter.CoordType.GPS);
// 转换
                            converter.coord(latLng);
// 获取转换之后的高德坐标
                            LatLng result = converter.convert();
                            String vehiID = carData.getVehiID();
                            String Heading = carData.getHeading();
                            String substring = Heading.substring(0, 2);
                            String substring2 = Heading.substring(2, 4);
                            Heading = substring2 + substring;
                            int parseInt = Integer.parseInt(Heading, 16);
//                            Log.e("车辆航向角Heading", "" + parseInt);
                            shezhizhongxin2(result.latitude, result.longitude, vehiID, parseInt);
                            //当前车辆所在车道
                            String veh_lane = carData.getVeh_Lane();
                            Log.e("他车纬度latitude", "" + otherlatitude);
                            Log.e("他车经度longitude", "" + otherlongitude);
                            Log.e("他车车道", "车ID:" + vehiID + ",车Head:" + Heading);


//                            if (mycarlatitude == 0) {
//                                //本车的位置为空，则不做显示
//                                return;
//                            }
//                            //算角度
//                            double angle1 = GPSutils.getAngle1(mycarlatitude, mycarlongitude, otherlatitude, otherlongitude);
//
//
//                            //距离
//                            double distance = GPSutils.getDistance(mycarlatitude, mycarlongitude, otherlatitude, otherlongitude);
//                            double juli = distance * 2.33;
//                            if (juli > 750) {
//                                OtherCar_Utils.othercar_visi(1);
//                                return;
//                            } else {
//                                OtherCar_Utils.othercar_visi(2);
//                            }
//
//                            if (angle1 > 0 && angle1 <= 90) {
//                                Log.e("位置", "后：" + distance);
//                                //前
//                                OtherCar_Utils.moveothercar(Integer.parseInt(veh_lane), planeWar, -(int) juli);
//                            } else if (angle1 > 90 && angle1 <= 180) {
//                                Log.e("位置", "后：" + distance);
//
//                                //前
//                                OtherCar_Utils.moveothercar(Integer.parseInt(veh_lane), planeWar, -(int) juli);
//                            } else if (angle1 > 180 && angle1 <= 270) {
//                                Log.e("位置", "前：" + distance);
//                                OtherCar_Utils.moveothercar(Integer.parseInt(veh_lane), planeWar, (int) juli);
//                                //后
//                            } else if (angle1 > 270 && angle1 <= 360) {
//                                Log.e("位置", "前：" + distance);
//                                OtherCar_Utils.moveothercar(Integer.parseInt(veh_lane), planeWar, (int) juli);
//                                //后
//                            }


                            //別人
                        }
                        Log.e("MessageEvent", "event:" + vehichlInfo8);

                    } else {
                        Log.e("MessageEvent", "event数据为空");
                    }
                    break;
                default:
                    break;
            }
        }
    }

    //0为无预警  1为有预警
    int typehongdeng = 0;
    int typexiansu = 0;
    int typeluce = 0;
    int typeother = 0;

    /**
     * 路况预警
     *
     * @param early_warning
     */
    public void AlertType_Byte1(int early_warning) {
        Log.e("路况预警数据AlertType_Byte1:", "" + early_warning);
        typeluce = 0;
        switch (early_warning) {

            case 37:

                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.ic_zywx);
                tvTraffic.setText("危险");
                Log.e("路测警示", "危险");
                typeluce = 1;
                break;
            case 15:
                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.ic_zyls);
                tvTraffic.setText("落石");
                Log.e("路测警示", "落石");
                typeluce = 1;
                break;
            case 02:

                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.ic_jw);
                imYindao.setVisibility(View.VISIBLE);
                tvTraffic.setText("急弯");
                Log.e("路测警示", "急弯");
                typeluce = 1;
                break;
            case 38:

                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.ic_sg);
                tvTraffic.setText("正在施工");
                Log.e("路测警示", "正在施工");
                typeluce = 1;
                break;
            case 17:

                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.ic_lh);
                tvTraffic.setText("路滑");
                Log.e("路测警示", "路滑");
                typeluce = 1;
                break;
            case 8:

                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.qiaoliang);
                tvTraffic.setText("前方桥梁预警");
                Log.e("路测警示", "前方桥梁");
                typeluce = 1;
                break;
            case 27:
                layouTrafficSign.setVisibility(View.VISIBLE);
                imYindao.setVisibility(View.VISIBLE);
                imYindao.setImageResource(R.drawable.ic_zywx);
                tvTraffic.setText("注意危险");
                Log.e("路测警示", "注意危险");
                typeluce = 1;
                break;


            default:
                typeluce = 0;
                break;
        }

    }

    int hongdengyujingxianshi1 = 0;
    int hongdengyujingxianshi2 = 0;
    int hongdengyujingxianshi3 = 0;

    public void AlertType_Byte2(String early_warning) {
        Log.e("路况预警数据AlertType_Byte2", "" + early_warning);
        typehongdeng = 0;
        typeother = 0;
        hongdengyujingxianshi1 = 0;
        hongdengyujingxianshi2 = 0;
        hongdengyujingxianshi3 = 0;
        if (early_warning.substring(7, 8).equals("1")) {
            intersection();
            tvTraffic.setText("交叉路口碰撞预警");
            imYindao.setVisibility(View.VISIBLE);
            imYindao.setImageDrawable(getResources().getDrawable(R.drawable.ic_crossroads_warn));
            Log.e("路测警示", "交叉路口碰撞预警");
            typeother = 1;
        } else {
            intersection();
            safe();
            Log.e("路测警示", "交叉路口无预警");

        }

        if (early_warning.substring(6, 7).equals("1")) {
//            intersection();
            imHongdeng1.setVisibility(View.VISIBLE);
            tv_hongdengtimename.setVisibility(View.VISIBLE);
            tv_hongdengtimename.setText("直行闯红灯预警");
            imHongdeng1.setImageDrawable(getResources().getDrawable(R.drawable.ic_straight_light_warn));
            Log.e("路测警示", "直行闯红灯预警");
            hongdengyujingxianshi1 = 1;
            typehongdeng = 1;
        } else {
            hongdengyujingxianshi1 = 0;
            intersection();
            safe();
            Log.e("路测警示", "直行闯红灯无预警");
//            tv_hongdengtimename.setVisibility(View.GONE);

        }
        if (early_warning.substring(5, 6).equals("1")) {
            intersection();
            imHongdeng1.setVisibility(View.VISIBLE);
            tv_hongdengtimename.setVisibility(View.VISIBLE);
            imHongdeng1.setImageDrawable(getResources().getDrawable(R.drawable.ic_leftlight_warn));
            Log.e("路测警示", "左转闯红灯预警");
            tv_hongdengtimename.setText("左转闯红灯预警");
            typehongdeng = 1;
            hongdengyujingxianshi2 = 1;
        } else {
            hongdengyujingxianshi2 = 0;
            intersection();
            safe();
//            tv_hongdengtimename.setVisibility(View.GONE);
            Log.e("路测警示", "左转闯红灯无预警");

        }
        if (early_warning.substring(4, 5).equals("1")) {
            intersection();
            imHongdeng1.setVisibility(View.VISIBLE);
            tv_hongdengtimename.setVisibility(View.VISIBLE);
            imHongdeng1.setImageDrawable(getResources().getDrawable(R.drawable.ic_rightlight_warn));
            Log.e("路测警示", "右转闯红灯预警");
            tv_hongdengtimename.setText("右转闯红灯预警");
            typehongdeng = 1;
            hongdengyujingxianshi3 = 1;
        } else {
            intersection();
            safe();
            Log.e("路测警示", "右转闯红灯无预警");
            hongdengyujingxianshi3 = 0;
//            tv_hongdengtimename.setVisibility(View.GONE);
        }
        if (early_warning.substring(3, 4).equals("1")) {
            imYindao.setVisibility(View.VISIBLE);
            imYindao.setImageDrawable(getResources().getDrawable(R.drawable.ic_infront_people_warn));
            Log.e("路测警示", "前方行人预警");
            tvTraffic.setText("前方行人预警");
            typeother = 1;

        } else {
            safe();
            Log.e("路测警示", "前方行人无预警");

        }
        if (early_warning.substring(2, 3).equals("1")) {
            imYindao.setVisibility(View.VISIBLE);
            imYindao.setImageDrawable(getResources().getDrawable(R.drawable.ic_speed_limit_warn));
            Log.e("路测警示", "限速预警");
            tvTraffic.setText("限速预警");
            typexiansu = 1;
        } else {
            safe();
            Log.e("路测警示", "限速无预警");
            typexiansu = 0;
        }
    }

    /**
     * 路况无预警
     */
    private void safe() {
//        imYindao.setVisibility(View.GONE);
//        tvTraffic.setText("无预警");
    }

    /**
     * 路况需要路口
     */
    private void intersection() {
//        mountain.setImageDrawable(getResources().getDrawable(R.drawable.v10));
////        tishi.setTextColor(getResources().getColor(R.color.red));
//        ViewAnimator.animate(mountain)
//                .translationY(-1000, 1800)
//                .interpolator(new DecelerateInterpolator())
//                .duration(20000)
//                .start();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (socket != null) {
            socket.stopUDPSocket();
        }
        mapView.onDestroy();

    }

    String jingshi = "";


    public void Description(String s) {
        if (typehongdeng == 0 && typexiansu == 0 && typeluce == 0 && typeother == 0) {
            //路况无预警
            Log.e("路况type0", "路况无预警");
            imYindao.setVisibility(View.VISIBLE);
            imYindao.setImageDrawable(getResources().getDrawable(R.drawable.zhengchang));
            tvTraffic.setText("路况暂无预警");

//            ================================
            imHongdeng.setVisibility(View.GONE);
            tvHongdengtime.setVisibility(View.GONE);
            tvLvdi.setVisibility(View.GONE);
            tvLvdi2.setVisibility(View.GONE);
            imHongdeng1.setVisibility(View.GONE);
            tv_hongdengtimename.setVisibility(View.GONE);
            return;
        }
        if (hongdengyujingxianshi2 == 0 && hongdengyujingxianshi1 == 0 && hongdengyujingxianshi3 == 0) {
            tv_hongdengtimename.setVisibility(View.GONE);
        }
//        else if (type == 1) {
//
//            //警示信息的距离(米)
//            long parseLong = Long.parseLong(s, 16);
//            Log.e("路测警示距离type1", "" + parseLong);
//            Spannable string = new SpannableString("前方" + parseLong + "米");
//            string.setSpan(new AbsoluteSizeSpan(70), 2, string.length() - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//            tvJuli.setText(string);
//        } else

        if (typehongdeng == 1) {
            tvHongdengtime.setVisibility(View.VISIBLE);
            tvLvdi.setVisibility(View.VISIBLE);
            tvLvdi2.setVisibility(View.VISIBLE);
            //红灯预警
            String baty1 = s.substring(4, 6);
            long l = Long.parseLong(baty1);
            if (l == 1) {
                //红灯
                Log.e("红灯预警type2", "红灯警示");
                jingshi = "红灯倒计时:";
                imHongdeng.setVisibility(View.VISIBLE);
                imHongdeng.setImageDrawable(getResources().getDrawable(R.drawable.hong));


            } else if (l == 2) {
                //黄灯
                Log.e("红灯预警type2", "黄灯警示");
                jingshi = "黄灯倒计时:";
                imHongdeng.setVisibility(View.VISIBLE);
                imHongdeng.setImageDrawable(getResources().getDrawable(R.drawable.huang));
            } else if (l == 3) {
                //绿灯
                Log.e("红灯预警type2", "绿灯警示");
                jingshi = "绿灯倒计时:";
                imHongdeng.setVisibility(View.VISIBLE);
                imHongdeng.setImageDrawable(getResources().getDrawable(R.drawable.lv));
            }
            String baty2 = s.substring(6, 8);
            //倒计时时间
            long l2 = Long.parseLong(baty2, 16);
            tvHongdengtime.setText(jingshi + l2);
            Log.e("红灯预警倒计时", jingshi + l2);

            String lvgao = s.substring(8, 10);
            //倒计时时间
            long lvgaos = Long.parseLong(lvgao, 16);

            String lvdi = s.substring(10, 12);
            //倒计时时间
            long lvdis = Long.parseLong(lvdi, 16);
            tvLvdi.setText("最大绿波通过速度:" + lvgaos + "KM/h");
            tvLvdi2.setText("最小绿波通过速度:" + lvdis + "KM/h");
        } else {
            imHongdeng.setVisibility(View.GONE);
            tvHongdengtime.setVisibility(View.GONE);
            tvLvdi.setVisibility(View.GONE);
            tvLvdi2.setVisibility(View.GONE);
            imHongdeng1.setVisibility(View.GONE);
//            tv_hongdengtimename.setVisibility(View.GONE);
        }

        if (typexiansu == 1) {

            //
            String baty1 = s.substring(0, 2);
            int v = Integer.parseInt(baty1);
            Log.e("限速类型", "" + v);
            String xiansu = "";
            //限速预警
            switch (v) {
                case 0:
                    Log.e("限速预警", "无限速");
                    xiansu = "无限速";
                    break;
                case 1:
                    Log.e("限速预警", "学校区域限速");
                    xiansu = "学校区域限速";
                    break;
                case 2:
                    Log.e("限速预警", "学校放学时的最大速度");
                    xiansu = "学校放学时的最大速度";
                    break;
                case 3:
                    Log.e("限速预警", "建筑区域最大速度");
                    xiansu = "建筑区域最大速度";
                    break;
                case 4:
                    Log.e("限速预警", "乘用车最小速度");
                    xiansu = "乘用车最小速度";
                    break;
                case 5:
                    Log.e("限速预警", "乘用车最大速度");
                    xiansu = "乘用车最大速度";
                    break;
                case 6:
                    Log.e("限速预警", "乘用车夜晚最大速度");
                    xiansu = "乘用车夜晚最大速度";
                    break;
                case 7:
                    Log.e("限速预警", "卡车最小速度");
                    xiansu = "卡车最小速度";
                    break;
                case 8:
                    Log.e("限速预警", "卡车最大速度");
                    xiansu = "卡车最大速度";
                    break;
                case 9:
                    Log.e("限速预警", "卡车夜晚最大速度");
                    xiansu = "卡车夜晚最大速度";
                    break;
                case 10:
                    Log.e("限速预警", "出租车最小速度");
                    xiansu = "出租车最小速度";
                    break;
                case 11:
                    Log.e("限速预警", "出租车最大速度");
                    xiansu = "出租车最大速度";
                    break;
                case 12:
                    Log.e("限速预警", "出租车最夜晚大速度");
                    xiansu = "出租车最夜晚大速度";
                    break;

                default:
                    break;
            }
            //限时速度
            String baty2 = s.substring(2, 4);
            int v2 = Integer.parseInt(baty2, 16);
            Log.e("限速", "" + xiansu + ":" + v2);
            if (xiansu.equals("无限速")) {
                tvTraffic.setText(xiansu);
            } else {
                tvTraffic.setText(xiansu + ":" + v2);
            }

        }

    }


    //    =======================================
    private MapView mapView;
    private AMap aMap;
    private UiSettings mUiSettings;
    private MyLocationStyle myLocationStyle;

    /**
     * 初始化AMap对象
     */
    private void init(Bundle savedInstanceState) {
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        if (aMap == null) {
            myLocationStyle = new MyLocationStyle();
            aMap = mapView.getMap();
            mUiSettings = aMap.getUiSettings();
            mUiSettings.setMyLocationButtonEnabled(false); // 是否显示默认的定位按钮
            aMap.setMyLocationEnabled(false);

            aMap.setMinZoomLevel(8);
            aMap.moveCamera(CameraUpdateFactory.zoomTo(20));
            aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER));

        }
        setMapCustomStyleFile(this);
        aMap.setMapType(AMap.MAP_TYPE_NAVI);//导航地图模式
    }

    private CustomMapStyleOptions mapStyleOptions = new CustomMapStyleOptions();

    private void setMapCustomStyleFile(Context context) {
        String styleName = "style_new.data";
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(styleName);
            byte[] b = new byte[inputStream.available()];
            inputStream.read(b);

            if (mapStyleOptions != null) {
                // 设置自定义样式
                mapStyleOptions.setStyleData(b);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    int v = 0;

    public void shezhizhongxin(double lat, double Lng, int parseInt) {
        v++;
        clearMarkers("A1");
        LatLng latLng = new LatLng(lat, Lng);
//        if (v==20) {
//
//            v=0;
//
//        }
//        if (v==1) {
        aMap.moveCamera(CameraUpdateFactory.zoomTo(20));
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
//        }


        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.cheche1
        ));
        markerOptions.icon(bitmapDescriptor);
        int zhi = 360 - (parseInt / 10);
//        Log.e("值",""+zhi);
        markerOptions.rotateAngle(zhi);

        Marker marker = aMap.addMarker(markerOptions);
        marker.setObject("A1");

    }

    //删除指定Marker
    private void clearMarkers(String marks) {

        //获取地图上所有Marker
        List<Marker> mapScreenMarkers = aMap.getMapScreenMarkers();
        Log.e("size", "数量：" + mapScreenMarkers.size());
        for (int i = 0; i < mapScreenMarkers.size(); i++) {
            Marker marker = mapScreenMarkers.get(i);
            if (marker.getObject() != null && marker.getObject().equals(marks)) {
                marker.remove();//移除当前Marker
            }
        }

    }


    public void shezhizhongxin2(double lat, double Lng, String vehiID, int parseInt) {
        clearMarkers(vehiID);
        LatLng latLng = new LatLng(lat, Lng);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.cheche2));
        markerOptions.icon(bitmapDescriptor);
        Log.e("原始zh", "xx" + parseInt);
        int zhi = 360 - (parseInt / 10);
        Log.e("值", "" + zhi);
        markerOptions.rotateAngle(zhi);
        Marker marker = aMap.addMarker(markerOptions);
        marker.setObject(vehiID);
    }

}
